<?php

class Cart extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    //后台列表页
    public function index()
    {
        $act = $this->input->get_post('ajaxdata');
        $result = array('code' => 201, 'message' => '', 'result' => '');

        switch ($act) {
            case 'changeCart':
                $gid = $this->input->get_post('id');
                $num = $this->input->get_post('num');

                $cartData = array();
                if (count($gid) > 0) {
                    foreach ($gid as $key => $val) {
                        $cartData[$val] = $num[$key];
                    }
                }
                sCookie('user_cart', json_encode($cartData));
                $result['code'] = 200;
                $result['message'] = '';
                break;
            case 'delCart':
                $spec = $this->input->get_post('id');
                if (count($spec) > 0) {
                    $cartData = json_decode(gCookie('user_cart'), true);
                    foreach ($spec as $val) {
                        if (isset($cartData[$val])) {
                            unset($cartData[$val]);
                        }
                    }
                    sCookie('user_cart', json_encode($cartData));
                    $result['code'] = 200;
                } else {
                    $result['code'] = 201;
                }
                break;
            case 'sendCart':
                $spec = $this->input->get_post('id');
                if (count($spec) > 0) {
                    $cartData = json_decode(gCookie('user_cart'), true);
                    $sendCart = array();
                    foreach ($spec as $val) {
                        if (isset($cartData[$val])) {
                            $num = $cartData[$val];
                            $pro = explode('_', $val);
                            $sendCart[] = $pro[0] . ';' . $num . ';' . $this->lg_user['channelId'] . ';' . $this->lg_user['enterpriseId'] . ';' . $pro[1];
                            unset($cartData[$val]);
                        }
                    }

                    $this->load->library('c_cart_send');
                    $sendData = array();
                    $sendData['password'] = $this->lg_user['password'];
                    $sendData['uid'] = $this->lg_user['id'];
                    $sendData['userType'] = $this->lg_user['type'];
                    $sendData['proList'] = implode(',', $sendCart);

                    //制造接口请求
                    $url = $this->c_cart_send->set_call_items($sendData);
                    $param = $this->c_cart_send->get_call_items();
                    $rs = $this->restcurl->post($url, $param);

                    if ($rs['ErrorCode'] == 0 && $rs['Success'] == 1 && $rs['ErrorMsg'] == '') {
                        $this->c_cart_send->set_back_items($rs['CallInfo']);
                        $orderInfo = $this->c_cart_send->get_back_items();
                        foreach ($orderInfo as $key => $val) {
                            $result['result'][$key] = json_encode($val);
                        }
                        $result['code'] = 200;
                    } else {
                        $result['code'] = 201;
                        $result['message'] = '操作失败';
                    }
                } else {
                    $result['code'] = 201;
                    $result['message'] = '请选择要结算的产品';
                }
                break;
            case 'buyNow':
                $id = $this->input->get_post('id');
                $num = $this->input->get_post('num');
                $pro = explode('_', $id);

                $pid = $pro[0];
                $sku_id = $pro[1];
                if ($pid && $sku_id && $num) {
                    $thisPro = $pid . ';' . $num . ';' . $this->lg_user['channelId'] . ';' . $this->lg_user['enterpriseId'] . ';' . $sku_id;
                    $this->load->library('c_cart_send');
                    $sendData = array();
                    $sendData['password'] = $this->lg_user['password'];
                    $sendData['uid'] = $this->lg_user['id'];
                    $sendData['userType'] = $this->lg_user['type'];
                    $sendData['proList'] = $thisPro;

                    //制造接口请求
                    $url = $this->c_cart_send->set_call_items($sendData);
                    $param = $this->c_cart_send->get_call_items();
                    $rs = $this->restcurl->post($url, $param);

                    if ($rs['ErrorCode'] == 0 && $rs['Success'] == 1 && $rs['ErrorMsg'] == '') {
                        $this->c_cart_send->set_back_items($rs['CallInfo']);
                        $orderInfo = $this->c_cart_send->get_back_items();
                        foreach ($orderInfo as $key => $val) {
                            $result['result'][$key] = json_encode($val);
                        }
                        $result['code'] = 200;
                    } else {
                        $result['code'] = 201;
                        $result['message'] = $rs['ErrorMsg'];
                    }
                } else {
                    $result['code'] = 201;
                    $result['message'] = '参数错误';
                }
                break;
            default:
                break;
        }
        echo json_encode($result);
        die;
    }

}
